<template>
	<view>
		<unicloud-db ref="happyRef" v-slot:default="{data, loading, error, options}" :collection="collection" getcount
			@load="loadData">
			<view v-if="error">{{error.message}}</view>
			<view v-else>
				<uni-list>
					<uni-list-item  v-for="item in data" :key="item._id">
						<template v-slot:body>
							<view style="flex:1">
								<view class="flex m-b">
									<view style="font-size: 12px;">订单编号:{{item.order_no}}</view>
									<view style="font-size: 12px;">
										<uni-dateformat :date="item.create_time" format='yyyy/MM/dd' />
									</view>
								</view>
								<view class="flex m-b">
									<text>{{item.uid[0]?.nickname}}</text>
									<text>{{item.price/100}}</text>
									<text>￥{{item.profit_price/100}}</text>
									<text>{{item.type?'进行中...':'失效'}}</text>
									<view class="tixian" v-if="item.type"  @click="txClick(item._id)">提现</view>
								</view>
							</view>
						</template>
					</uni-list-item>
					<uni-load-more :status="more" @clickLoadMore="clickLoadMore"
						:content-text="contentText"></uni-load-more>
				</uni-list>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	import {
		mapState
	} from "pinia"
	import {
		useUserStore
	} from "@/pinia/user.js"
	import logo from "@/static/logo.png"
	const db = uniCloud.database();
	export default {
		props:{
			openid:[String]
		},
		data() {
			return {
				more: "more",
				contentText: {
					contentdown: '点击加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},
		computed: {
			...mapState(useUserStore, ['userInfo']),
			collection() {
				return [
					db.collection('wolf-profit-divvy').where(`pid=="${this.userInfo._id}"`).field('uid,order_no,price,type,profit_price,create_time').orderBy('create_time desc')
					.getTemp(),
					db.collection('uni-id-users').field("_id,nickname,avatar_file").getTemp()
				]
			}
		},
		methods: {
			loadData(data, e) {
				console.log("data-happiness=====>>>>>>",data);
				if (e) {
					this.more = "no-more"
				} else {
					this.more = "more"
				}

			},
			clickLoadMore(e) {
				if (e.detail.status == "more") {
					this.$refs.happyRef.loadMore()
				}
			},
			navigator(id) {
				// console.log("id===>>>",id);
				// if (!id) return
				// uni.navigateTo({
				// 	url: `/pages/list/userInfo?id=${id}`
				// })
			},
			
			
			async txClick(id){
				if(!this.openid || !this.userInfo._id){
					return uni.showToast({
						title:"提现失败，请晚会在试试",
						icon:'none'
					})
				}
				uni.showLoading({
					title:"提现中...",
					icon:'none',
					mask:true
				})
				let ok_t = await db.collection('wolf-profit-divvy').doc(id).get()
				if(!ok_t.result.data[0]?.type){
					this.$refs.happyRef.refresh()
					return uni.showToast({
						title:"此订单已失效",
						icon:'none',
						mask:true
					})
				}
				await db.collection('wolf-profit-divvy').doc(id).update({
					type:0
				})
				
				let price = 300 * 100 - 300 * 0.6 - 300
				if (price <= 0) {
					price = 0
				}
				
				let now = new Date().getTime() //获取当前时间戳
				let code = '' + (parseInt(Math.random() * 1000000) + 1000000)
				code = code.substring(1, 7)
				let order_no = "TX" + code + now //业务订单号
				let out_batch_no = order_no + 'x1'
				let obj = {
					img: '',
					price: 30000,
					con_price: price,
					status: 0,
					uid: this.userInfo._id,
					order_id: order_no,
					create_time: now,
					out_batch_no,
					type: 0,
					openid:this.openid,
					name:''
				}
				db.collection('wolf-tixian').add(obj).then(res => {
					uni.showToast({
						title: '已提交',
						icon: "none"
					})
				})
				
				this.$refs.happyRef.refresh()
				uni.hideLoading()
			},
		}
	}
</script>

<style scoped lang="scss">
	.flex{
		display: flex;
		justify-content: space-between;
	}
	.m-b{
		margin-bottom: 20rpx;
	}
	.tixian{
		padding: 2rpx 10rpx;
		border: 1px solid #b303aa;
		background-color: #b303aa;
		color: #fff;
		border-radius: 5rpx;
		font-size: 14px;
	}
</style>